{% extends 'base.html' %}
{% block title %}日志管理 - {{ SITE_NAME }}{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>操作日志管理</h2>
    </div>

    <!-- 过滤表单 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-2">
                    <label class="form-label">操作类型</label>
                    <select name="operation_type" class="form-select" onchange="this.form.submit()">
                        <option value="">全部类型</option>
                        {% for type in operation_types %}
                            <option value="{{ type }}" {% if current_filters.operation_type == type %}selected{% endif %}>{{ type }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">对象类型</label>
                    <select name="content_type" class="form-select" onchange="this.form.submit()">
                        <option value="">全部对象</option>
                        {% for type in content_types %}
                            <option value="{{ type }}" {% if current_filters.content_type == type %}selected{% endif %}>{{ type }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">开始日期</label>
                    <input type="date" name="start_date" class="form-control" value="{{ current_filters.start_date }}" onchange="this.form.submit()">
                </div>
                <div class="col-md-2">
                    <label class="form-label">结束日期</label>
                    <input type="date" name="end_date" class="form-control" value="{{ current_filters.end_date }}" onchange="this.form.submit()">
                </div>
                <div class="col-md-2">
                    <label class="form-label">用户名</label>
                    <input type="text" name="username" class="form-control" value="{{ current_filters.username }}" placeholder="搜索用户名">
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary w-100"><i class="bi bi-search me-2"></i>搜索</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 日志列表 -->
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>用户</th>
                            <th>操作类型</th>
                            <th>对象类型</th>
                            <th>对象名称</th>
                            <th>操作时间</th>
                            <th>IP地址</th>
                            <th>详情</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for log in page_obj %}
                        <tr>
                            <td>{{ log.user.username }}</td>
                            <td>{{ log.get_operation_type_display }}</td>
                            <td>{{ log.content_type }}</td>
                            <td>{{ log.object_name }}</td>
                            <td>{{ log.timestamp|date:"Y-m-d H:i:s" }}</td>
                            <td>{{ log.ip_address }}</td>
                            <td>{{ log.details|default:"-" }}</td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="7" class="text-center">没有找到匹配的日志记录</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页控件 -->
            {% if page_obj.has_other_pages %}
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page=1{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" aria-label="First">
                                <span aria-hidden="true">&laquo;&laquo;</span>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    {% endif %}

                    {% for num in page_obj.paginator.page_range %}
                        {% if page_obj.number == num %}
                            <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                            <li class="page-item"><a class="page-link" href="?page={{ num }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">{{ num }}</a></li>
                        {% endif %}
                    {% endfor %}

                    {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" aria-label="Last">
                                <span aria-hidden="true">&raquo;&raquo;</span>
                            </a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}